package com.gibs.peixunapi.service.impl;

import com.gibs.peixunapi.VO.CourseInfoVO;
import com.gibs.peixunapi.dao.CourseDao;
import com.gibs.peixunapi.dao.UserDao;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.model.Course;
import com.gibs.peixunapi.model.User;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.service.CourseService;
import com.gibs.peixunapi.utils.ConverterUtil;
import com.gibs.peixunapi.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CourseServiceImpl implements CourseService{

	@Autowired
	private CourseDao courseDao;
	@Autowired
	private UserDao userDao;

	@Transactional
@Override
	public Result create(CourseInfoVO courseInfoVO) {
		Course course = new Course();
		ConverterUtil.copyProperties(courseInfoVO,course,"assitTeacherList","teacher");
		User teacher = userDao.findById(courseInfoVO.getTeacherId()).orElseThrow(EntityNotFoundException::new);
		List<User> assitTeacherList = new ArrayList<>();
		courseInfoVO.getAssitTeachersIdList().forEach(
				e->assitTeacherList.add(userDao.findById(e).orElseThrow(EntityNotFoundException::new)));

		//TODO course_teacher分离出来
		course.setTeacher(teacher);
//		course.setAssitTeachers();
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);

	}

	@Transactional
@Override
	public Result save(@Valid Course object) {
		try {
			
//			Course course = courseDao.findOne(object.getId());
//			ConverterUtil.copyNotNullProperties(object,course);
//			courseDao.save(course);
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				courseDao.delete(Integer.valueOf(str));
//			}
			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


@Override
	public Result  get(Integer id) {
		try {
//			HashMap<String, Object> data = new HashMap<String, Object>();
//			Course object = courseDao.findOne(id);
//			if (object == null) {
//				Result(false, Status.FAILD, "查找失败", null);
//			}
//			HashMap<String,Object> map = new HashMap();
//			map.put("id", object.getId());
//			map.put("name", object.getName()==null?"":object.getName());
//			map.put("summary", object.getSummary()==null?"":object.getSummary());
//			map.put("creatorId", object.getCreatorId()==null?"":object.getCreatorId());
//			map.put("hours", object.getHours()==null?"":object.getHours());
//			HashMap<String,Object> startTime = new HashMap<String,Object>();
//			startTime.put("name", object.getStartTime()==null?"":object.getStartTime().getName());
//			startTime.put("id", object.getStartTime()==null?"":object.getStartTime().getId());
//			map.put("startTime", startTime);
//			map.put("endTime", object.getEndTime()==null?"":DateUtils.toString(object.getEndTime()));
//			map.put("invalidTime", object.getInvalidTime()==null?"":DateUtils.toString(object.getInvalidTime()));
//			map.put("type", object.getType()==null?"":object.getType());
//			HashMap<String,Object> teacher = new HashMap<String,Object>();
//			teacher.put("name", object.getTeacher()==null?"":object.getTeacher().getName());
//			teacher.put("id", object.getTeacher()==null?"":object.getTeacher().getId());
//			map.put("teacher", teacher);
//			HashMap<String,Object> assitTeachers = new HashMap<String,Object>();
//			assitTeachers.put("name", object.getAssitTeachers()==null?"":object.getAssitTeachers().getName());
//			assitTeachers.put("id", object.getAssitTeachers()==null?"":object.getAssitTeachers().getId());
//			map.put("assitTeachers", assitTeachers);
//			map.put("fileCount", object.getFileCount()==null?"":object.getFileCount());
//			map.put("videoCount", object.getVideoCount()==null?"":object.getVideoCount());
//			HashMap<String,Object> isDelete = new HashMap<String,Object>();
//			isDelete.put("name", object.getIsDelete()==null?"":object.getIsDelete().getName());
//			isDelete.put("id", object.getIsDelete()==null?"":object.getIsDelete().getId());
//			map.put("isDelete", isDelete);
//			map.put("sort", object.getSort()==null?"":object.getSort());
//			data.put("object",map);

			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public Result  getList(String key) {
		try {
//			List<Course> list = new ArrayList<>();
//			if (!StringUtils.isEmpty(key)) {
//				list = courseDao.findLike("%" + key + "%");
//			} else {
//				list = courseDao.findAll();
//			}
//			if (list.size() == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(Course object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				map.put("name", object.getName()==null?"":object.getName());
//				map.put("summary", object.getSummary()==null?"":object.getSummary());
//				map.put("creatorId", object.getCreatorId()==null?"":object.getCreatorId());
//				map.put("hours", object.getHours()==null?"":object.getHours());
//				HashMap<String,Object> startTime = new HashMap<String,Object>();
//				startTime.put("name", object.getStartTime()==null?"":object.getStartTime().getName());
//				startTime.put("id", object.getStartTime()==null?"":object.getStartTime().getId());
//				map.put("startTime", startTime);
//				map.put("endTime", object.getEndTime()==null?"":DateUtils.toString(object.getEndTime()));
//				map.put("invalidTime", object.getInvalidTime()==null?"":DateUtils.toString(object.getInvalidTime()));
//				map.put("type", object.getType()==null?"":object.getType());
//				HashMap<String,Object> teacher = new HashMap<String,Object>();
//				teacher.put("name", object.getTeacher()==null?"":object.getTeacher().getName());
//				teacher.put("id", object.getTeacher()==null?"":object.getTeacher().getId());
//				map.put("teacher", teacher);
//				HashMap<String,Object> assitTeachers = new HashMap<String,Object>();
//				assitTeachers.put("name", object.getAssitTeachers()==null?"":object.getAssitTeachers().getName());
//				assitTeachers.put("id", object.getAssitTeachers()==null?"":object.getAssitTeachers().getId());
//				map.put("assitTeachers", assitTeachers);
//				map.put("fileCount", object.getFileCount()==null?"":object.getFileCount());
//				map.put("videoCount", object.getVideoCount()==null?"":object.getVideoCount());
//				HashMap<String,Object> isDelete = new HashMap<String,Object>();
//				isDelete.put("name", object.getIsDelete()==null?"":object.getIsDelete().getName());
//				isDelete.put("id", object.getIsDelete()==null?"":object.getIsDelete().getId());
//				map.put("isDelete", isDelete);
//				map.put("sort", object.getSort()==null?"":object.getSort());
//				data.add(map);
//			}
			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result getListPage(String key, Integer current, Integer limit) {
		try {
//			List<Course> list = new ArrayList<>();
//			limit = limit == null ? 10 : limit;
//			current = current == null ? 1 : current;
//			int offset = (current - 1) * limit;
//			Long total = null;
//			if (!StringUtils.isEmpty(key)) {
//				list = courseDao.findPageLike("%" + key + "%", offset, limit);
//				total = courseDao.countLike("%" + key + "%");
//			} else {
//				list = courseDao.findPage(offset, limit);
//				total = courseDao.count();
//			}
//			if (total == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(Course object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				map.put("name", object.getName()==null?"":object.getName());
//				map.put("summary", object.getSummary()==null?"":object.getSummary());
//				map.put("creatorId", object.getCreatorId()==null?"":object.getCreatorId());
//				map.put("hours", object.getHours()==null?"":object.getHours());
//				HashMap<String,Object> startTime = new HashMap<String,Object>();
//				startTime.put("name", object.getStartTime()==null?"":object.getStartTime().getName());
//				startTime.put("id", object.getStartTime()==null?"":object.getStartTime().getId());
//				map.put("startTime", startTime);
//				map.put("endTime", object.getEndTime()==null?"":DateUtils.toString(object.getEndTime()));
//				map.put("invalidTime", object.getInvalidTime()==null?"":DateUtils.toString(object.getInvalidTime()));
//				map.put("type", object.getType()==null?"":object.getType());
//				HashMap<String,Object> teacher = new HashMap<String,Object>();
//				teacher.put("name", object.getTeacher()==null?"":object.getTeacher().getName());
//				teacher.put("id", object.getTeacher()==null?"":object.getTeacher().getId());
//				map.put("teacher", teacher);
//				HashMap<String,Object> assitTeachers = new HashMap<String,Object>();
//				assitTeachers.put("name", object.getAssitTeachers()==null?"":object.getAssitTeachers().getName());
//				assitTeachers.put("id", object.getAssitTeachers()==null?"":object.getAssitTeachers().getId());
//				map.put("assitTeachers", assitTeachers);
//				map.put("fileCount", object.getFileCount()==null?"":object.getFileCount());
//				map.put("videoCount", object.getVideoCount()==null?"":object.getVideoCount());
//				HashMap<String,Object> isDelete = new HashMap<String,Object>();
//				isDelete.put("name", object.getIsDelete()==null?"":object.getIsDelete().getName());
//				isDelete.put("id", object.getIsDelete()==null?"":object.getIsDelete().getId());
//				map.put("isDelete", isDelete);
//				map.put("sort", object.getSort()==null?"":object.getSort());
//				data.add(map);
//			}
//			PageData pageData = new PageData(total, limit, current);
			return ResultUtil.successPage(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	

@Override
	public Result getOptions(String key) {
		try {
//			List<Course> list = new ArrayList<>();
//			if (!StringUtils.isEmpty(key)) {
//				list = objectDao.findLike("%" + key + "%");
//			} else {
//				list = objectDao.findAll();
//			}
//			List<Object> data = new ArrayList<>();
//			for (Course object : list) {
//				HashMap<String, Object> map = new HashMap();
//				map.put("value", object.getId());
//				map.put("name", object.getName());
//				data.add(map);
//			}
			return ResultUtil.success(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


}

